home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 2.iso
/
toolbox
/
public
/
TIFF
/
tools
/
pal2rgb.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
7KB
|
251 lines
#ifndef lint
static char rcsid[] = "$Header: /toolbox/src/irix5/public/TIFF/tools/RCS/pal2rgb.c,v 1.1 1996/03/08 00:37:40 dave Exp $";
#endif
/*
* Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
* Copyright (c) 1991, 1992 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tiffio.h"
#define howmany(x, y) (((x)+((y)-1))/(y))
#define streq(a,b) (strcmp(a,b) == 0)
#define CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
#define CopyField3(tag, v1, v2, v3) \
if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
void usage(void);
static int
checkcmap(int n, uint16* r, uint16* g, uint16* b)
{
while (n-- > 0)
if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
return (16);
fprintf(stderr, "Warning, assuming 8-bit colormap.\n");
return (8);
}
void
main(int argc, char* argv[])
{
uint16 bitspersample, samplesperpixel, shortv;
uint32 imagewidth, imagelength;
uint16 config = PLANARCONFIG_CONTIG;
uint16 compression = -1;
uint32 rowsperstrip = -1;
float floatv;
char *stringv;
uint32 longv;
uint16 *rmap, *gmap, *bmap;
uint32 row;
tsample_t s;
int cmap = -1;
TIFF *in, *out;
argc--, argv++;
if (argc < 2)
usage();
for (; argc > 2 && argv[0][0] == '-'; argc--, argv++) {
if (streq(argv[0], "-none")) {
compression = COMPRESSION_NONE;
continue;
}
if (streq(argv[0], "-packbits")) {
compression = COMPRESSION_PACKBITS;
continue;
}
if (streq(argv[0], "-lzw")) {
compression = COMPRESSION_LZW;
continue;
}
if (streq(argv[0], "-contig")) {
config = PLANARCONFIG_CONTIG;
continue;
}
if (streq(argv[0], "-separate")) {
config = PLANARCONFIG_SEPARATE;
continue;
}
if (streq(argv[0], "-8bit")) {
cmap = 8;
continue;
}
if (streq(argv[0], "-16bit")) {
cmap = 16;
continue;
}
if (streq(argv[0], "-rowsperstrip")) {
argc--, argv++;
rowsperstrip = atoi(argv[0]);
continue;
}
usage();
}
in = TIFFOpen(argv[0], "r");
if (in == NULL)
exit(-1);
if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &shortv) ||
shortv != PHOTOMETRIC_PALETTE) {
fprintf(stderr, "%s: Expecting a palette image.\n", argv[0]);
exit(-1);
}
if (!TIFFGetField(in, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
fprintf(stderr,
"%s: No colormap (not a valid palette image).\n",
argv[0]);
exit(-1);
}
out = TIFFOpen(argv[1], "w");
if (out == NULL)
exit(-2);
CopyField(TIFFTAG_SUBFILETYPE, longv);
CopyField(TIFFTAG_IMAGEWIDTH, imagewidth);
CopyField(TIFFTAG_IMAGELENGTH, imagelength);
CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);
if (bitspersample != 8) {
fprintf(stderr, "%s: Sorry, can only handle 8-bit images.\n",
argv[0]);
exit(-1);
}
if (compression != (uint16)-1)
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
else
CopyField(TIFFTAG_COMPRESSION, compression);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
CopyField(TIFFTAG_ORIENTATION, shortv);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
CopyField(TIFFTAG_PREDICTOR, shortv);
CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
CopyField(TIFFTAG_XRESOLUTION, floatv);
CopyField(TIFFTAG_YRESOLUTION, floatv);
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
if (rowsperstrip <= 0)
rowsperstrip = (8*1024)/TIFFScanlineSize(out);
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
rowsperstrip == 0 ? 1 : rowsperstrip);
CopyField(TIFFTAG_XPOSITION, floatv);
CopyField(TIFFTAG_YPOSITION, floatv);
CopyField(TIFFTAG_ARTIST, stringv);
CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
CopyField(TIFFTAG_MAKE, stringv);
CopyField(TIFFTAG_MODEL, stringv);
CopyField(TIFFTAG_SOFTWARE, stringv);
CopyField(TIFFTAG_DATETIME, stringv);
CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
CopyField(TIFFTAG_PAGENAME, stringv);
CopyField(TIFFTAG_DOCUMENTNAME, stringv);
(void) TIFFGetField(in, TIFFTAG_PLANARCONFIG, &shortv);
if (cmap == -1)
cmap = checkcmap(1<<bitspersample, rmap, gmap, bmap);
if (cmap == 16) {
/*
* Convert 16-bit colormap to 8-bit.
*/
int i;
for (i = (1<<bitspersample)-1; i > 0; i--) {
#define CVT(x) (((x) * 255) / ((1L<<16)-1))
rmap[i] = CVT(rmap[i]);
gmap[i] = CVT(gmap[i]);
bmap[i] = CVT(bmap[i]);
}
}
{ unsigned char *ibuf, *obuf;
register unsigned char* pp;
register uint32 x;
ibuf = (unsigned char*)malloc(TIFFScanlineSize(in));
obuf = (unsigned char*)malloc(TIFFScanlineSize(out));
switch (config) {
case PLANARCONFIG_CONTIG:
for (row = 0; row < imagelength; row++) {
if (!TIFFReadScanline(in, ibuf, row, 0))
goto done;
pp = obuf;
for (x = 0; x < imagewidth; x++) {
*pp++ = rmap[ibuf[x]];
*pp++ = gmap[ibuf[x]];
*pp++ = bmap[ibuf[x]];
}
if (!TIFFWriteScanline(out, obuf, row, 0))
goto done;
}
break;
case PLANARCONFIG_SEPARATE:
for (row = 0; row < imagelength; row++) {
if (!TIFFReadScanline(in, ibuf, row, 0))
goto done;
for (pp = obuf, x = 0; x < imagewidth; x++)
*pp++ = rmap[ibuf[x]];
if (!TIFFWriteScanline(out, obuf, row, 0))
goto done;
for (pp = obuf, x = 0; x < imagewidth; x++)
*pp++ = gmap[ibuf[x]];
if (!TIFFWriteScanline(out, obuf, row, 0))
goto done;
for (pp = obuf, x = 0; x < imagewidth; x++)
*pp++ = bmap[ibuf[x]];
if (!TIFFWriteScanline(out, obuf, row, 0))
goto done;
}
break;
}
free(ibuf);
free(obuf);
}
done:
(void) TIFFClose(in);
(void) TIFFClose(out);
}
void
usage(void)
{
fprintf(stderr, "usage: pal2rgb [options] input output\n");
fprintf(stderr, "where options are:\n");
fprintf(stderr,
" -contig\tpack samples contiguously (e.g. RGBRGB...)\n");
fprintf(stderr,
" -separate\tstore samples separately (e.g. RRR...GGG...BBB...)\n");
fprintf(stderr, "\n");
fprintf(stderr,
" -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
fprintf(stderr,
" -packbits\tcompress output with packbits encoding\n");
fprintf(stderr,
" -none\t\tuse no compression algorithm on output\n");
fprintf(stderr, "\n");
fprintf(stderr,
" -8bit\tassume 8-bit colormap values (instead of 16-bit)\n");
fprintf(stderr,
" -rowsperstrip #\tmake each strip have no more than # rows\n");
exit(-1);
}